<template>
  <div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="API名称" prop="apiName">
        <el-input
          v-model="queryParams.resourceName"
          placeholder="请输入API名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row type="flex" justify="space-between" style="width: 100%;">
      <el-row :gutter="20" style="width: 100%;">
        <div class="widget-center-draggable-wrapper">
          <el-empty v-if="this.total===0" description="暂无资源"></el-empty>
          <el-col v-for="(item) in dataApiList" :key="item.id" :gutter="20" class="box-card-col" >
            <el-card :body-style="{ padding: '8px'}" style="max-height: 240px" :gutter="20" class="box-card-item">
              <el-descriptions :title="item.apiName" :column="4" border>
                <el-descriptions-item>
                  <template slot="label">
                    <i class="el-icon-office-building"></i>
                    来源部门
                  </template>
                  {{item.createDept}}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template slot="label">
                    <i class="el-icon-mobile-phone"></i>
                    主题
                  </template>
                  {{item.cataName}}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template slot="label">
                    <i class="el-icon-date"></i>
                    发布时间
                  </template>
                  {{item.publishDate}}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template slot="label">
                    <i class="el-icon-date"></i>
                    更新日期
                  </template>
                  {{item.updateDate}}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template slot="label">
                    <i class="el-icon-mobile-phone"></i>
                    状态
                  </template>
                  <el-tag :type="item.status==='1'?'success':item.status==='2'?'warning':''">{{statusFormatter(item.status)}}</el-tag>
                </el-descriptions-item>
                <el-descriptions-item>
                  <template slot="label">
                    <i class="el-icon-mobile-phone"></i>
                    描述
                  </template>
                  {{item.resourceComment}}
                </el-descriptions-item>
              </el-descriptions>
              <div style="padding: 10px;">
                <div class="bottom clearfix">
                  <el-button-group style=" float: right; margin-right: 0px">
                    <el-button  size="medium" v-if="item.status==='0'" icon="el-icon-view" @click="handleCheckStatus(item)" style=" float: right; margin-right: 0px">
                      <span>查看进度</span>
                    </el-button>
                    <el-button  size="medium" icon="el-icon-view" @click="handleDetail(item)" style=" float: right; margin-right: 5px">
                      <span>查看详情</span>
                    </el-button>
                    <el-button  size="medium"  v-if="item.status!=='0'&&item.isSubscribed!=='1'" icon="el-icon-view" @click="handleSubscribe(item)" style=" float: right; margin-right: 5px">
                      <span>订阅</span>
                    </el-button>
                  </el-button-group>
                </div>
              </div>
            </el-card>
          </el-col>
        </div>
      </el-row>
    </el-row>
    <flow-image v-if="dialogFlowImageVisible" :visible.sync="dialogFlowImageVisible" :processInstanceId="currentProcessInstanceId"></flow-image>
    <el-dialog :close-on-click-modal="false" title="接口详情" :visible.sync="showApiInfo" width="60%">
      <data-api-info ref="ref-api" :apiId="currentRow.resourceId"></data-api-info>
    </el-dialog>

  </div>
</template>
<script>
  import {listUserResource, pageUserResource, applySubmit, updateUserResource} from '@/api/market/userresource'
  import DataApiInfo from "@/views/market/dataapi/DataApiInfo";
  import FlowImage from '@/views/workflow/instance/components/FlowImage'

  export default {
    name: 'MyApiList',
    components: {  DataApiInfo, FlowImage },
    data() {
      return {
        // 流程图
        dialogFlowImageVisible: false,
        currentProcessInstanceId: '',
        showApiInfo: false,
        currentRow: {resourceId: ''},
        tableHeight: document.body.offsetHeight - 310 + 'px',
        // 展示切换
        showOptions: {
          data: {},
          showList: true,
          showAdd: false,
          showEdit: false,
          showDetail: false,
          showExample: false,
          showBind: false
        },
        // 遮罩层
        loading: false,
        // 表格头
        tableColumns: [
          { prop: 'apiName', label: 'API名称', show: true },
          { prop: 'apiVersion', label: 'API版本', show: true },
          { prop: 'apiUrl', label: 'API路径', show: true },
          { prop: 'reqMethod', label: '请求类型', show: true },
          { prop: 'resType', label: '返回格式', show: true },
          {
            prop: 'status',
            label: '状态',
            show: true
          },
          { prop: 'createTime', label: '创建时间', show: true },
          { prop: 'isSubscribed', label: '是否订阅', show: true },
        ],
        // 默认选择中表格头
        checkedTableColumns: [],
        tableSize: 'medium',
        // 状态数据字典
        statusOptions: [],
        // 数据集表格数据
        dataApiList: [],
        // 总数据条数
        total: 0,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 20,
          resourceName: '',
          resourceType: 'api'
        }
      }
    },
    created() {
      this.getDicts('data_user_resource_status').then(response => {
        if (response.success) {
          this.statusOptions = response.data
        }
      })
      this.getList()
    },
    mounted() {
      this.initCols()
    },
    methods: {

      handleSubscribe(row) {
        this.$confirm('您正在订阅该资源, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const userResource = {
            id: row.id,
            resourceId: row.resourceId,
            resourceType: 'api',
            isSubscribed: '1'
          }
          updateUserResource(userResource).then(response => {
            this.loading = false
            if (response.success) {
              this.$message({
                type: 'success',
                message: '订阅成功'
              });
              this.getList()
            }
          })
        }).catch(() => {
          this.getList()
        });

      },
      handleCancelSubscribe(row) {
        this.$confirm('您正在取消订阅该资源, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const userResource = {
            id: row.id,
            resourceId: row.resourceId,
            resourceType: 'api',
            isSubscribed: '0'
          }
          updateUserResource(userResource).then(response => {
            this.loading = false
            if (response.success) {
              this.$message({
                type: 'success',
                message: '取消订阅成功'
              });
            }
          })
        }).catch(() => {

        });
        getList()
      },
      /** 查询数据Api列表 */
      getList() {
        this.loading = false
        pageUserResource(this.queryParams).then(response => {
          this.loading = false
          if (response.success) {
            const { data } = response
            this.dataApiList = response.data.data
            this.total = data.total
            this.$emit('freshCount', 'api',this.total)
          }
        })
      },
      initCols() {
        this.checkedTableColumns = this.tableColumns.map(col => col.prop)
      },
      handleCheckedColsChange(val) {
        this.tableColumns.forEach(col => {
          if (!this.checkedTableColumns.includes(col.prop)) {
            col.show = false
          } else {
            col.show = true
          }
        })
      },
      handleCommand(command) {
        this.tableSize = command
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams = {
          pageNum: 1,
          pageSize: 20,
          apiName: ''
        }
        this.handleQuery()
      },
      /** 刷新列表 */
      handleRefresh() {
        this.getList()
      },

      /** 详情按钮操作 */
      handleDetail(row) {
        this.showApiInfo = true
        this.currentRow = row
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`)
        this.queryParams.pageNum = 1
        this.queryParams.pageSize = val
        this.getList()
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`)
        this.queryParams.pageNum = val
        this.getList()
      },
      statusFormatter( cellValue) {
        const dictLabel = this.selectDictLabel(this.statusOptions, cellValue)
        return dictLabel
      },
      /** 详情按钮操作 */
      handleCheckStatus(row) {
        this.currentProcessInstanceId = row.processInstanceId
        this.dialogFlowImageVisible = true
      },
    }
  }
</script>
<style lang="scss" scoped>
  .right-toolbar {
    float: right;
  }
</style>
